package com.fire.control.system.config.socketIo;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.fire.control.system.config.redis.RedisService;
import com.fire.control.system.dto.SocketClientInfo;
import com.fire.control.system.dto.SocketMessageInfo;
import com.fire.control.system.statics.Key;  
  
@Component  
public class SocketIoMessageEventHandler   
{  
    @Autowired private SocketIOServer server;
      
    @Autowired  private RedisService redisService;
    
    
    public void addClient(String userId){
    	SocketClientInfo clientInfo = new SocketClientInfo();
        redisService.setObject(Key.redis_key_lora_soket_client_id_+userId, clientInfo,Key.redis_key_lora_soket_client_id_.getExpire());
    }
    
    
    //添加connect事件，当客户端发起连接时调用，本文中将clientid与sessionid存入数据库  
    //方便后面发送消息时查找到对应的目标client,  
    @OnConnect 
    public void onConnect(SocketIOClient client)  
    {  
        String clientId = client.getHandshakeData().getSingleUrlParam(Key.socket_client_id.toString());
        SocketClientInfo clientInfo = redisService.getObject(Key.redis_key_lora_soket_client_id_+clientId, SocketClientInfo.class);
        if (clientInfo != null){
            clientInfo.setConnected(true);
            clientInfo.setLastConnectedDate(new Date(System.currentTimeMillis()));
            clientInfo.setSessionId(client.getSessionId().toString());
            redisService.setObject(Key.redis_key_lora_soket_client_id_+clientId, clientInfo,3600*24*30);
        }else{ 
        	clientInfo = new SocketClientInfo();
        	clientInfo.setConnected(true);
            clientInfo.setLastConnectedDate(new Date(System.currentTimeMillis()));
            clientInfo.setSessionId(client.getSessionId().toString());
            redisService.setObject(Key.redis_key_lora_soket_client_id_+clientId, clientInfo,3600*24*30);
        }
    }  
      
    //添加@OnDisconnect事件，客户端断开连接时调用，刷新客户端信息  
    @OnDisconnect   
    public void onDisconnect(SocketIOClient client)  
    {  
        String clientId = client.getHandshakeData().getSingleUrlParam("clientId");  
        SocketClientInfo clientInfo = redisService.getObject(Key.redis_key_lora_soket_client_id_+clientId, SocketClientInfo.class);
        if (clientInfo != null){
            clientInfo.setConnected(false); 
            clientInfo.setSessionId(null);
            redisService.setObject(Key.redis_key_lora_soket_client_id_+clientId, clientInfo,0); 
        }  
    }  
      
    //消息接收入口，当接收到消息后，查找发送目标客户端，并且向该客户端发送消息，且给自己发送消息  
    @OnEvent(value = "messageevent")  
    public void onEvent(SocketIOClient client, AckRequest request, SocketMessageInfo data)   
    {  
        /*String targetClientId = data.getTargetClientId();  
        ClientInfo clientInfo = clientInfoRepository.findClientByclientid(targetClientId);  
        if (clientInfo != null && clientInfo.getConnected() != 0)  
        {  
            UUID uuid = new UUID(clientInfo.getMostsignbits(), clientInfo.getLeastsignbits());  
            //System.out.println(uuid.toString());  
            MessageInfo sendData = new MessageInfo();  
            sendData.setSourceClientId(data.getSourceClientId());  
            sendData.setTargetClientId(data.getTargetClientId());  
            sendData.setMsgType("chat");  
            sendData.setMsgContent(data.getMsgContent());  
            System.out.println(client.getSessionId().toString());
            client.sendEvent("messageevent", sendData);  
            //server.getClient(uuid).sendEvent("messageevent", sendData);
            server.getClient(UUID.fromString(clientInfo.getSessionId())).sendEvent("messageevent", sendData);
        }  */
    }  
    
    public void send(String clientId,String msg){
    	SocketClientInfo clientInfo = redisService.getObject(Key.redis_key_lora_soket_client_id_+clientId, SocketClientInfo.class);
    	if (clientInfo != null && clientInfo.isConnected()){
    		 server.getClient(UUID.fromString(clientInfo.getSessionId())).sendEvent(Key.socket_client_id.toString(), msg);
    	}
    }
    
}  